<?php
namespace app\jobs;

use app\components\printMachine\PrintFactory;
use app\config\Constants;
use app\models\GoodsOrder;
use app\models\Merchant;
use app\models\PrintMachine;
use yii\helpers\Json;

/**
 * 打印任务
 * Class PrintJob
 * @package app\jobs
 */
class PrintJob{

    private static $handle_type = [Constants::ORDER_ACCEPT,Constants::EVENT_TYPE_ORDER_REPEAT_PRINT];

    public function execute($queue,$job){
        if($job['msg_type'] != Constants::MSG_TYPE_ORDER){
            return true;
        }
        if(!in_array($job['event_type'],self::$handle_type)){
            return true;
        }

        return $this->accept($job);
    }

    //商家接单
    public function accept($job){
        $order = GoodsOrder::findOne(['order_no'=>$job['data']['order_no']]);
        if(!$order){
            return false;
        }
        $print = PrintMachine::findOne(['merchant_id'=>$order->merchant_id]);
        //打印机状态
        if(!$print || $print->status == Constants::STATUS_PROHIBIT){
            return true;
        }
        $merchant = Merchant::findOne(['merchant_id'=>$order->merchant_id]);
        if(!$merchant){
            return false;
        }
        //营业时间
        $range = explode("-",$merchant->business_time);
        if($job['event_type'] == Constants::ORDER_ACCEPT && date("H") < $range[0] && date("H") > $range[1]){
            return true;
        }

        return $this->print($order,$print);
    }

    public function print(GoodsOrder $order,PrintMachine $print){
        $delivery_type = [
            Constants::DELIVERY_MERCHANT=>'商家自配',
            Constants::DELIVERY_PLATFORM=>'平台配送',
        ];

        $pay_method = [
            Constants::PAY_DELIVERY => '货到付款',
            Constants::PAY_WECHAT => '微信',
            Constants::PAY_ALIPAY => '支付宝',
        ];

        $header['merchant_name'] = $order->merchant_name;
        $header['address'] = $order->address;
        $header['name'] = $order->name;
        $header['mobile'] = $order->mobile;
        $header['add_time'] = $order->add_time;
        $header['remark'] = $order->remark;
        $header['total_price'] = $order->total_price;
        $header['coupon_price'] = $order->coupon_price;
        $header['real_price'] = $order->real_price;
        $header['delivery_type'] = $delivery_type[$order->merchant->delivery_type];
        $header['delivery_price'] = $order->delivery_price;
        $header['delivery_time'] = $order->delivery_time;
        $header['pay_method'] = $pay_method[$order->pay_method];
        $header['order_no'] = $order->order_no;

        $data = [];
        foreach(Json::decode($order->goods_item) as $v){
            $goods_name = $v['goods_name'];
            if(isset($v['attr_names'])){
                $goods_name .= "(";
                foreach($v['attr_names']as $vv){
                    $goods_name .= $vv . "-";
                }
                $goods_name = rtrim($goods_name,'-');
                $goods_name .= ")";
            }

            $data[] = [
                'title'=>$goods_name,
                'price'=>$v['price'],
                'num'=>$v['new_count']
            ];
        }

        if(YII_DEBUG){
            return true;
        }
        $provider = PrintFactory::getInstance($print->type);
        return $provider->print($header,$data,$print->sn,$print->type,$print->count);
    }
}